package com.sls.web.manager.action.recharge;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.json.annotations.JSON;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.WebTools;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.recharge.pojo.Area;
import com.sls.web.manager.recharge.pojo.NativeRechargeRule;
import com.sls.web.manager.recharge.pojo.PhoneType;
import com.sls.web.manager.recharge.pojo.SupplierRechargeMode;
import com.sls.web.manager.recharge.querybean.AreaQuery;
import com.sls.web.manager.recharge.querybean.NativeRechargeRuleQuery;
import com.sls.web.manager.recharge.querybean.SupplierRechargeModeQuery;
import com.sls.web.manager.recharge.service.AreaService;
import com.sls.web.manager.recharge.service.NativeRechargeRuleService;
import com.sls.web.manager.recharge.service.OperationService;
import com.sls.web.manager.recharge.service.PhoneTypeService;
import com.sls.web.manager.recharge.service.SupplierRechargeModeService;

/**
 * 充值通道管理
 * 
 * @author sxf
 * 
 */
public class RechargeChannelAction extends SuperAction {
	private static final long serialVersionUID = 1426434953711579466L;

	@Resource(name = "areaService")
	private AreaService areaService;
	@Resource(name = "operationService")
	private OperationService operationService;
	@Resource(name = "nativeRechargeRuleService")
	private NativeRechargeRuleService service;
	@Resource(name = "supplierRechargeModeService")
	private SupplierRechargeModeService supplierRechargeModeService;
	@Resource(name = "phoneTypeService")
	private PhoneTypeService phoneTypeService;

	private List<PhoneType> phoneTypeList;// 号码类型
	private List<NativeRechargeRule> nativeRechargeRuleList;
	private List<RechargeChannelAction.RchargeChannel> channelList;

	private String provinceCode;// 省份代码area
	private String areaCityCode;// 城市代码area
	private String operationCode;// 运营商代码
	private String phoneTypeCode;// 选中的号码类型

	private static final int CHANNEL_NUMBER = 20;

	private int channelNumber;// 通道数量

	private AreaQuery areaQuery;
	private Pagination<RchargeChannel> page;

	// private Map<String, String> areaMap;

	/**
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {

		String isSearch = WebTools.getParameter("isSearch");
		List<PhoneType> phoneTypeListTemp = new ArrayList<PhoneType>();

		phoneTypeList = phoneTypeService.listEnable(getCurrentOperator());

		for (Iterator<PhoneType> it = phoneTypeList.iterator(); it.hasNext();) {
			PhoneType oper = (PhoneType) it.next();
			if ("00".equals(oper.getCode())) {
				it.remove();// 过滤全部
			}
		}

		channelNumber = CHANNEL_NUMBER;
		if (StringUtils.isNotBlank(phoneTypeCode)) {
			for (Iterator<PhoneType> it = phoneTypeList.iterator(); it
					.hasNext();) {
				PhoneType oper = (PhoneType) it.next();
				if (phoneTypeCode.equals(oper.getCode())) {
					phoneTypeListTemp.add(oper);
				}
			}
		} else {
			phoneTypeListTemp = phoneTypeList;
		}

		if (areaQuery == null) {
			areaQuery = new AreaQuery();
			areaQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			if (phoneTypeListTemp != null && !phoneTypeListTemp.isEmpty()) {
				areaQuery.setPageSize(3 * (int) Math.ceil(phoneTypeListTemp
						.size() * 1.0 / 2));
			} else {
				areaQuery.setPageSize(Constants.INIT_PAGESIZE);
			}
		} else {
			if (areaQuery != null && areaQuery.getPageSize() != null) {
				areaQuery.setPageSize(areaQuery.getPageSize()
						/ phoneTypeList.size());
			}
		}

		// List<Area> provinceList = areaService.queryAllList(1,
		// getCurrentOperator());
		// List<Area> areaCityList = areaService.queryAllList(2,
		// getCurrentOperator());
		// areaMap = new LinkedHashMap<String, String>();
		// for (Area province : provinceList) {
		// areaMap.put(province.getCode(), province.getAreaName() +
		// " ------");
		// for (Area areaCity : areaCityList) {
		// if (areaCity.getParentCode().equals(province.getCode())) {
		// areaMap.put(
		// province.getCode() + "-" + areaCity.getCode(),
		// province.getAreaName() + " "
		// + areaCity.getAreaName());
		// }
		// }
		// }
		if (StringUtils.isNotBlank(isSearch)) {

			if (StringUtils.isNotBlank(provinceCode)) {
				areaQuery.setParentCode(provinceCode);
			}
			if (StringUtils.isNotBlank(areaCityCode)) {
				areaQuery.setCode(areaCityCode);
			}
			areaQuery.setGrade(2);// 查询地级市
			List<Area> areaList = new ArrayList<Area>();
			Pagination<Area> areaPage = areaService.queryPage(areaQuery,
					getCurrentOperator());
			if (areaPage != null && areaPage.getList() != null
					&& !areaPage.getList().isEmpty()) {
				areaList = areaPage.getList();
			}
			areaQuery.setPageSize(areaQuery.getPageSize()
					* phoneTypeList.size());

			NativeRechargeRuleQuery nrrq = new NativeRechargeRuleQuery();
			if (areaList != null && !areaList.isEmpty()) {
				List<String> nrrqAreaCodeList = new ArrayList<String>();
				for (Area area : areaList) {
					nrrqAreaCodeList.add(area.getCode());
				}
				nrrq.setAreaCodeList(nrrqAreaCodeList);
			}

			List<NativeRechargeRule> ruleAllList = service.queryAllList(nrrq,
					getCurrentOperator());
			channelList = new LinkedList<RechargeChannelAction.RchargeChannel>();
			for (Area area : areaList) {
				for (PhoneType dict : phoneTypeListTemp) {
					RchargeChannel rc = new RchargeChannel();
					if (area != null && area.getGrade() == 2) {
						AreaQuery aq = new AreaQuery();
						aq.setCode(area.getParentCode());
						Area provinceArea = areaService.queryOneInMemcache(aq,
								getCurrentOperator());
						rc.setProvinceArea(provinceArea);
						rc.setAreaCity(area);
						rc.setAreaName(provinceArea.getAreaName() + " "
								+ area.getAreaName());
					}
					rc.setPhoneType(dict);
					List<NativeRechargeRule> ruleList = new ArrayList<NativeRechargeRule>();
					for (NativeRechargeRule nativeRechargeRule : ruleAllList) {
						if (nativeRechargeRule.getCode().equals(area.getCode())
								&& nativeRechargeRule.getPhoneType().equals(
										dict.getCode())) {
							ruleList.add(nativeRechargeRule);
						}
					}
					rc.setList(ruleList);
					channelList.add(rc);
				}
			}

			page = new Pagination<RechargeChannelAction.RchargeChannel>(
					areaPage.getCurrentPage(), areaPage.getRowCount()
							* phoneTypeListTemp.size(), areaPage.getPageSize()
							* phoneTypeListTemp.size(), channelList);
		}
		return "list";
	}

	public String doAddOrUpdate() throws Exception {
		String areaCode = WebTools.getParameter("areaCode");// 区域代码
		String phoneType = WebTools.getParameter("phoneTypeCode");// 号码类型
		String nativeRchargeRuleId = WebTools
				.getParameter("nativeRchargeRuleId");// 本地充值供货规则编码
		String supplierRechargeModeId = WebTools
				.getParameter("supplierRechargeModeId");// 供货商提供的充值方式编号
		String sort = WebTools.getParameter("sort");// 排序
		String enable = WebTools.getParameter("enable");// 是否启用
		if (StringUtils.isNotBlank(nativeRchargeRuleId)) {
			// 更新操作
			NativeRechargeRule info = new NativeRechargeRule();
			info.setId(nativeRchargeRuleId);
			info.setSupplierRechargeModeId(supplierRechargeModeId);
			info.setPhoneType(phoneType);
			try {
				info.setSort(Integer.valueOf(sort));
			} catch (Exception e) {

			}
			if ("1".equals(enable) || "true".equals(enable)) {
				info.setEnable(true);
			} else if ("0".equals(enable) || "false".equals(enable)) {
				info.setEnable(false);
			}

			service.update(info, getCurrentOperator());
		} else {
			// 插入操作
			// Operation oper = operationService.findByCode(operationCode,
			// getCurrentOperator());
			NativeRechargeRule info = new NativeRechargeRule();
			info.setSupplierRechargeModeId(supplierRechargeModeId);
			info.setPhoneType(phoneType);
			info.setCode(areaCode);
			info.setEnable(true);
			info.setDeleted(false);
			info.setUpdateOperator(getCurrentOperator().getUserId());
			if (StringUtils.isNotBlank(supplierRechargeModeId)) {
				SupplierRechargeMode srm = supplierRechargeModeService
						.queryById(supplierRechargeModeId, getCurrentOperator());
				if (srm != null) {
					info.setOperationCode(srm.getOperationCode());
				}
			}

			try {
				info.setSort(Integer.valueOf(sort));
			} catch (Exception e) {

			}
			nativeRchargeRuleId = service.insert(info, getCurrentOperator());
		}
		Map<String, Object> ajaxMap = new LinkedHashMap<String, Object>();
		ajaxMap.put("result", "success");
		ajaxMap.put("nativeRechargeRuleId", nativeRchargeRuleId);
		// WebTools.writeAjax(map);
		message = WebTools.writeAjax(ajaxMap);
		return "ajax";
	}

	public String delete() throws Exception {
		String nativeRchargeRuleId = WebTools
				.getParameter("nativeRchargeRuleId");
		if (StringUtils.isNotBlank(nativeRchargeRuleId)) {
			NativeRechargeRule info = new NativeRechargeRule();
			info.setId(nativeRchargeRuleId);
			service.delete(info, getCurrentOperator());
		}
		Map<String, Object> ajaxMap = new LinkedHashMap<String, Object>();
		ajaxMap.put("result", "success");
		ajaxMap.put("nativeRechargeRuleId", nativeRchargeRuleId);
		// WebTools.writeAjax(map);
		message = WebTools.writeAjax(ajaxMap);
		return "ajax";
	}

	/**
	 * 页面充值通道查询
	 * 
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String supplierRechargeModeList() throws Exception {
		String phoneType = WebTools.getParameter("phoneType");// 号码类型
		String areaCode = WebTools.getParameter("areaCode");// 区域代码
		SupplierRechargeModeQuery rQuery = new SupplierRechargeModeQuery();
		if (StringUtils.isNotBlank(phoneType)) {
			List<String> phoneTypeList = new ArrayList<String>();
			phoneTypeList.add(phoneType);			
			phoneTypeList.add("00");
			rQuery.setPhoneTypeList(phoneTypeList);
			rQuery.setEnable(true);
		}
		if (StringUtils.isNotBlank(areaCode)) {
			// TODO
		}
		List<SupplierRechargeMode> supplierRechargeModeList = null;
		Pagination<SupplierRechargeMode> page = (Pagination<SupplierRechargeMode>) supplierRechargeModeService
				.queryList(rQuery, getCurrentOperator());
		if (page != null) {
			supplierRechargeModeList = page.getList();
		}
		// WebTools.writeAjax(supplierRechargeModeList);
		message = WebTools.writeAjax(supplierRechargeModeList);
		return "ajax";
	}

	public List<NativeRechargeRule> getNativeRechargeRuleList() {
		return nativeRechargeRuleList;
	}

	public void setNativeRechargeRuleList(
			List<NativeRechargeRule> nativeRechargeRuleList) {
		this.nativeRechargeRuleList = nativeRechargeRuleList;
	}

	/**
	 * 页面显示的帮助类,在内部显示
	 * 
	 * @author sxf
	 * 
	 */
	public class RchargeChannel implements Serializable {
		private static final long serialVersionUID = 2520082938158652582L;

		// area 表
		private Area provinceArea;
		private Area areaCity;
		private String areaName;
		// 号码类型
		private PhoneType phoneType;

		private SupplierRechargeMode mode;
		// 通道列表
		private List<NativeRechargeRule> list;

		public List<NativeRechargeRule> getList() {
			return list;
		}

		public void setList(List<NativeRechargeRule> list) {
			this.list = list;
		}

		public Area getProvinceArea() {
			return provinceArea;
		}

		public void setProvinceArea(Area provinceArea) {
			this.provinceArea = provinceArea;
		}

		public Area getAreaCity() {
			return areaCity;
		}

		public void setAreaCity(Area areaCity) {
			this.areaCity = areaCity;
		}

		public String getAreaName() {
			return areaName;
		}

		public void setAreaName(String areaName) {
			this.areaName = areaName;
		}

		public SupplierRechargeMode getMode() {
			return mode;
		}

		public void setMode(SupplierRechargeMode mode) {
			this.mode = mode;
		}

		public PhoneType getPhoneType() {
			return phoneType;
		}

		public void setPhoneType(PhoneType phoneType) {
			this.phoneType = phoneType;
		}
	}

	@JSON(serialize = false)
	public List<RechargeChannelAction.RchargeChannel> getChannelList() {
		return channelList;
	}

	public void setChannelList(
			List<RechargeChannelAction.RchargeChannel> channelList) {
		this.channelList = channelList;
	}

	@JSON(serialize = false)
	public String getProvinceCode() {
		return provinceCode;
	}

	public void setProvinceCode(String provinceCode) {
		this.provinceCode = provinceCode;
	}

	@JSON(serialize = false)
	public String getAreaCityCode() {
		return areaCityCode;
	}

	public void setAreaCityCode(String areaCityCode) {
		this.areaCityCode = areaCityCode;
	}

	@JSON(serialize = false)
	public String getOperationCode() {
		return operationCode;
	}

	public void setOperationCode(String operationCode) {
		this.operationCode = operationCode;
	}

	@JSON(serialize = false)
	public List<PhoneType> getPhoneTypeList() {
		return phoneTypeList;
	}

	public void setPhoneTypeList(List<PhoneType> phoneTypeList) {
		this.phoneTypeList = phoneTypeList;
	}

	public int getChannelNumber() {
		return channelNumber;
	}

	public void setChannelNumber(int channelNumber) {
		this.channelNumber = channelNumber;
	}

	@JSON(serialize = false)
	public Pagination<RchargeChannel> getPage() {
		return page;
	}

	public void setPage(Pagination<RchargeChannel> page) {
		this.page = page;
	}

	@JSON(serialize = false)
	public AreaQuery getAreaQuery() {
		return areaQuery;
	}

	public void setAreaQuery(AreaQuery areaQuery) {
		this.areaQuery = areaQuery;
	}

	public String getPhoneTypeCode() {
		return phoneTypeCode;
	}

	public void setPhoneTypeCode(String phoneTypeCode) {
		this.phoneTypeCode = phoneTypeCode;
	}

}
